﻿using System;
using System.Web;
using Melee.Controller.Request;
using Melee.Controller.Storage;
using StructureMap;

namespace Melee.Controller.Handlers
{
    public class AjaxHandler : AsyncHandlerBase
    {
        public override IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, object extraData)
        {
            WebRequest webRequest = ObjectFactory.GetInstance<IWebRequestFactory>().CreateWebRequestFrom(cb, context);
            try
            {
                ObjectFactory.GetInstance<IHandlerFactory>().GetHandlers().Handle(webRequest);
            }
            catch (Exception ex)
            {
                webRequest.Storage.Add(HttpStorageKeys.Exception, ex);
                webRequest.Storage.Add(HttpStorageKeys.Message, "操作失败,请稍后再尝试!");
                webRequest.AsyncResult.SetCompleted();
            }
            return webRequest.AsyncResult;
        }
    }
}
